package com.atguigu.spzx.product.controller;

import com.atguigu.spyx.model.common.Result;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.h5.OrderItem;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.ProductSku;
import com.atguigu.spyx.model.dto.h5.ProductSkuDto;
import com.atguigu.spyx.model.vo.h5.ProductSkuVo;
import com.atguigu.spzx.product.service.ProductService;
import com.atguigu.spzx.product.service.ProductSkuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LT
 * @create 2023-11-18-8:47
 */
@RestController
@RequestMapping("/api/product")
public class ProductSkuController {

    @Resource
    ProductSkuService productSkuService;

    @PutMapping("/updateSalesAndStock")
    public Result updateSalesAndStock(@RequestBody List<OrderItem> orderItems){
        productSkuService.updateSalesAndStock(orderItems);
        return Result.ok();
    }


    @GetMapping("/{page}/{limit}")
    public Result page(@PathVariable("page")Integer page,
                       @PathVariable("limit")Integer limit,
                        ProductSkuDto productSkuDto){
        PageInfo<ProductSku> pageInfo = productSkuService.page(page,limit,productSkuDto);

        return Result.ok().data(pageInfo);
    }

    @GetMapping("/item/{skuId}")
    public Result findSkuById(@PathVariable("skuId")Long skuId){
        ProductSkuVo productSkuVo = productSkuService.findSkuById(skuId);
        return Result.ok().data(productSkuVo);
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id")Long id){
       ProductSku sku = productSkuService.findById(id);
       return Result.ok().data(sku);
    }
}
